<?php
/**
 * Description of NewsController
 * @author : Yeu tinh
 * @since Oct 19, 2012
 */
class NewsController extends AppController {

    public $name = 'News';
    public $uses = array('News', 'Catalogue');
    public $type = 'new';

    public function beforeFilter() {
        parent::beforeFilter();
        $this->set('no_slide', 'no_slide');
        $this->set('no_backlink', 'no_backlink');
    }

    public function index($id = null){
        if(empty($id)){
            if(isset($this->notice['not_exits_link'])) $this->Session->setFlash($this->notice['not_exits_link'], 'default', array('class' => 'alert alert-error'));
            $this->redirect(DOMAIN);
            exit;
        }
        $detailNews = $this->Catalogue->findByLink($id);
        if(empty($detailNews)){
            if(isset($this->notice['not_exits_link'])) $this->Session->setFlash($this->notice['not_exits_link'], 'default', array('class' => 'alert alert-error'));
            $this->redirect(DOMAIN);
            exit;
        }
        
        //set title. keyword, decription
        if(!empty($detailNews['Catalogue']['title_seo'])) $this->set('title_for_layout', $detailNews['Catalogue']['title_seo']);
        else $this->set('title_for_layout', $detailNews['Catalogue']['name']);
        if($detailNews['Catalogue']['meta_key']) $this->set('keywords_for_layout', $detailNews['Catalogue']['meta_key']);
        if($detailNews['Catalogue']['meta_des']) $this->set('description_for_layout', $detailNews['Catalogue']['meta_des']);

        //set limmit
        $limit  = 20;
        
        $mnId = array();
        $mnId = $this->multiMenuNews($detailNews['Catalogue']['id'], null);
        $mnId[$detailNews['Catalogue']['id']] = $detailNews['Catalogue']['id'];
        if($detailNews['Catalogue']['parent_id'] == 13) $mnId[$detailNews['Catalogue']['parent_id']] = $detailNews['Catalogue']['parent_id'];
   
        $this->paginate = array(
            'conditions'=>array(
                'News.status'=>1,
                'News.cat_id'=>$mnId
                ),
                'limit' => $limit,
                'order' => 'News.order DESC'
            );
	    $this->set('list', $this->paginate('News',array()));

        $this->set('cat', $detailNews);
    }

    public function detail($id = null) {
        if(empty($id)){
            if(isset($this->notice['not_exits_link'])) $this->Session->setFlash($this->notice['not_exits_link'], 'default', array('class' => 'alert alert-error'));
            $this->redirect(DOMAIN);
            exit;
        }
        $detail = $this->News->findByLink(substr($id, 0, -5));
        if(empty($detail)){
            if(isset($this->notice['not_exits_link'])) $this->Session->setFlash($this->notice['not_exits_link'], 'default', array('class' => 'alert alert-error'));
            $this->redirect(DOMAIN);
            exit;
        }
        
        $cat = $this->Catalogue->findById($detail['News']['cat_id']);
        $this->set('cat', $cat);
        $this->set('detail', $detail);

        //set kieu binh luan
        $this->set('id_post', $detail['News']['id']);
        $this->set('type_post', 'post');

        //set title, keyword, desciption
        if($detail['News']['title_seo'] != null){
            if($detail['News']['title_seo']) $this->set('title_for_layout', $detail['News']['title_seo']);
        }else{
            if($detail['News']['name']) $this->set('title_for_layout', $detail['News']['name']);
        }
        if($detail['News']['meta_key'])$this->set('keywords_for_layout', $detail['News']['meta_key']);
        if($detail['News']['meta_des'])$this->set('description_for_layout', $detail['News']['meta_des']);

        //set limmit
        $limit  = 8;

        $this->paginate = array(
            'conditions'=>array(
                    'News.status'=>1,
                    'News.id <>' => $detail['News']['id'],
                    'News.cat_id' => $detail['News']['cat_id']
                ),
                'order' => 'News.order DESC','limit' => $limit
            );
	    $this->set('tinlienquan', $this->paginate('News',array()));
    }

    public function multiMenuNews($parentid = null, $trees = NULL) {
        $parmenu = $this->Catalogue->find('all', array(
            'conditions' => array(
                'Catalogue.parent_id' => $parentid,
                'Catalogue.status' => 1
            ),
            'order' => 'Catalogue.order ASC'
            ));
        if (count($parmenu) > 0) {
            foreach ($parmenu as $field) {
                $trees[$field['Catalogue']['id']] = $field['Catalogue']['id'];
                $trees = $this->multiMenuNews($field['Catalogue']['id'], $trees);
            }
        }
        return $trees;
    }
}